# _*_ codeing : utf-8 _*_
# @Time : 2024-09-22
# @Author : wxp
# @File : json
# @Project : wxp-python

import json

if __name__ == '__main__':
    # Python 对象转换为 JSON 字符串
    data = {
        "person": {
            "name": "John Doe",
            "age": 35,
            "address": {
                "street": "123 Main St",
                "city": "Anytown",
                "zipcode": "12345"
            },
            "email": [
                "john.doe@example.com",
                "johndoe@gmail.com"
            ]
        },
        "is_active": True,
        "orders": [
            {
                "order_id": "A123",
                "products": [
                    {
                        "product_id": "P001",
                        "name": "Product 1",
                        "price": 19.99
                    },
                    {
                        "product_id": "P002",
                        "name": "Product 2",
                        "price": 29.99
                    }
                ]
            },
            {
                "order_id": "B456",
                "products": [
                    {
                        "product_id": "P003",
                        "name": "Product 3",
                        "price": 39.99
                    }
                ]
            }
        ]
    }
    json_string = json.dumps(data)
    print("JSON字符串：\n" + json_string)
    # JSON 字符串解析为 Python 对象
    parsed_data = json.loads(json_string)
    print("JSON对象：", type(parsed_data))
    print(parsed_data)
    print(parsed_data.get("person").get("name"))
